const express = require('express');

const morgan = require('morgan');   
const cors = require('cors');       //为客户端提供跨域资源请求

// require('./model/index');

const app = express();

const router = require('./router');
const error = require('./middleware/error'); //统一处理错误中间件

app.use(express.json());
app.use(express.urlencoded());
app.use(cors());
app.use(morgan('dev'));

app.use('/api', router);

//添加这个就解决了
// app.use(cors({
//     origin: ['http://localhost:8081'], //指定接受的地址
//     methods: ['GET', 'POST'], //指定接受的请求类型
//     allowedHeaders: ['Content-Type', 'Authorization'] //指定header
// }))
//设置跨域请求
// app.all('*', function (req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*");
//     res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
//     res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
//     res.header("X-Powered-By", ' 3.2.1')
//     res.header("Content-Type", "application/json;charset=utf-8");
//     next();
// });

const PORT = process.env.port || 7000;

//挂载错误处理中间件
app.use(error());

app.listen(PORT, () => {
    console.log(`服务器${PORT}启动成功！！！！`)
})